/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "Players.h"
#include "Player.h"
#include "Channel.h"
#include "../net/world/Party.h"
#include "Map.h"
#include "Maps.h"
#include "Event.h"
#include "../tools/PacketCreator.h"
#include "../tools/Timer.h"
#include "../tools/Run.h"
#include "../client/EventsData.h"
#include "../client/EventData.h"
#include "Events.h"

unsigned int Event::id;

class EventTimer : public Run {
private:
	Events* events;
	string name;
public:
	EventTimer(Events* events, string& name) : events(events), name(string(name)) {}
	void run(){
		events->deleteEvent(name, true);
	}
};

void Event::deleteEvent(Channel* channel, string name) {
	channel->getEvents()->deleteEvent(name, true);
}

Event::Event(Channel* channel, string name) : channel(channel), name(name), players(new Players()) {
	/*Timer::Id iD(Timer::Types::EventTimer, setId(), 0);
	if(channel->getTimers()->checkTimer(iD) > 0)
		channel->getTimers()->removeTimer(iD);
	new Timer::Timer(std::tr1::bind(&Event::deleteEvent, this, channel, name), iD, channel->getTimers(), Timer::Time::fromNow(EventsData::getInstance()->getDataByID(name)->getTime() * 1000));*/
	//timer = Timers::getInstance()->startTimer(EventsData::getInstance()->getDataByID(name)->getTime()*1000, new EventTimer(channel->getEvents(), name));
	party = false;
	deleted = false;
	vector <int>* maps = EventsData::getInstance()->getDataByID(name)->getMapsData()->getData();
	for(int i=0; i<(int)maps->size(); i++) {
		Map* map = channel->getMaps()->getMap((*maps)[i]);
		if(map != 0) {
			map->clear();
			map->setEvent(this);
		}
	}
}

Event::~Event() {
	if(party) {
		rparty->clear();
	}
	delete players;
}

void Event::end(bool timer) {
	struct ChangeMapEvent { 
		void operator()(Player* player) {
			if(player->getMap()->getEvent() == pevent)
				player->setMap(map);
		}
		int map;
		Event* pevent;
	}
	changeMap = {EventsData::getInstance()->getDataByID(name)->getEndMap(), this};
	players->run(&changeMap);
}

void Event::end(Player* player, bool timer) {
	if (player->getMap()->getEvent() != 0 && timer) {
		player->setMap(EventsData::getInstance()->getDataByID(name)->getEndMap());
	}
	players->remove(player);
}

void Event::start(Player* player) {
	player->setMap(EventsData::getInstance()->getDataByID(name)->getStartMap());
	players->add(player);
	//Timer::Id iD(Timer::Types::EventTimer, this->getId(), 0);
	//player->send(PacketCreator().showTimer(this->getChannel()->getTimers()->checkTimer(iD)));
}

int Event::getTime() {
    //Timer::Id iD(Timer::Types::EventTimer, this->getId(), 0);
	return 0;//this->getChannel()->getTimers()->checkTimer(iD);
}

void Event::setTimer(Timer* timer)
{
	this->timer = timer;
}

Timer* Event::getTimer()
{
	return timer;
}